package com.ccsoft.zknote.util;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;

import com.cc.commontools.entity.FieldInfo;
import com.cc.commontools.entity.TableInfo;
import com.ccsoft.zknote.pojo.User;
import com.ccsoft.zknote.pojo.system.BusinessException;
import com.ccsoft.zknote.pojo.system.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.cc.commontools.entity.DbConfig;
import com.cc.commontools.utils.MySQLUtils;
import com.cc.commontools.entity.DbInfo;

import java.sql.SQLException;
import java.util.List;
import java.util.Optional;

/**
 * @author chanchaw
 * @create 2023-07-11 15:41
 */
@Service
public class SystemUtils {
    public static final Integer VERIFICATION_CODE_DURATION = 15;// 验证码有效时长15分钟

    @Autowired
    private StringRedisTemplate redisTemplate;

    // 返回排除减号的UUID
    public static String getUUID(){
        return IdUtil.simpleUUID();
    }

    public static BusinessException notEmpty(String s,String tip){
        if(s == null) throw new BusinessException(tip);
        if(s.length() == 0) throw new BusinessException(tip);
        return null;
    }

    /**
     * 根据前端传递来的 token 查询获取 redis 中缓存的 user
     * @param token 前端传递来的 token，例如：userToken:xxxxx
     * @return 将 redis 中缓存的对象发序列化为 User 对象并返回
     */
    public User getRedisTokenUser(String token){
        String userString = redisTemplate.opsForValue().get(token);
        User user = null;
        try {
            user = JSONUtil.toBean(userString, User.class);
        } catch (Exception e) {
            throw new BusinessException("根据token" + token + "获取redis中缓存的用户并反序列化时出现异常");
        }
        // 前端要验证错误码10002，是则跳转到登录页面，其他错误码不需要跳转
        if(user == null) throw new BusinessException(ResultCode.LOGIN_INVALID);
        return user;
    }

    public List<String> test01() throws SQLException, ClassNotFoundException {
        DbConfig dbConfig = DbConfig.builder().build();
        List<String> showa2023 = MySQLUtils.getTableNameList(dbConfig, "showa2023");
        return showa2023;
    }

    public List<DbInfo> getDbInfoList() throws SQLException, ClassNotFoundException {
        DbConfig dbConfig = DbConfig.builder().build();
        List<DbInfo> dbInfoList = MySQLUtils.getDbInfoList(dbConfig);
        return dbInfoList;
    }

    public List<String> getDbNameList() throws SQLException, ClassNotFoundException {
        DbConfig dbConfig = DbConfig.builder().build();
        List<String> dbNameList = MySQLUtils.getDbNameList(dbConfig,null);
        return dbNameList;
    }

    public List<String> getDbNameList(DbConfig dbConfig) throws SQLException, ClassNotFoundException {
        List<String> dbNameList = MySQLUtils.getDbNameList(dbConfig,null);
        return dbNameList;
    }

    public List<String> getTableNameList(String dbName) throws SQLException, ClassNotFoundException {
        DbConfig dbConfig = DbConfig.builder().build();
        List<String> tableNameList = MySQLUtils.getTableNameList(dbConfig,dbName);
        return tableNameList;
    }

    // 获取指定名称表的信息以及其下的字段信息
    public TableInfo getTableInfoAndFields(DbConfig dbConfig, String dbName, String tableName) throws SQLException, ClassNotFoundException {
        return MySQLUtils.getTableInfoAndFields(dbConfig, dbName, tableName);
    }

    public List<TableInfo> getTableInfoList8DbName(String dbName) throws SQLException, ClassNotFoundException {
        dbName = Optional.ofNullable(dbName).orElse("");
        if(dbName.length() == 0) throw new BusinessException("没有指定数据库名称，无法获取表信息！");

        DbConfig dbConfig = DbConfig.builder().build();
        List<TableInfo> tableInfoList = MySQLUtils.getTableInfoList(dbConfig,dbName);
        return tableInfoList;
    }

    public List<FieldInfo> getFieldInfoList(String dbName, String tableName) throws SQLException, ClassNotFoundException {
        dbName = Optional.ofNullable(dbName).orElse("");
        if(dbName.length() == 0) throw new BusinessException("没有指定数据库名称，无法获取表信息！");
        tableName = Optional.ofNullable(tableName).orElse("");
        if(tableName.length() == 0) throw new BusinessException("没有指定表名，无法获取字段信息！");

        DbConfig dbConfig = DbConfig.builder().build();
        List<FieldInfo> fieldInfoList = MySQLUtils.getFieldInfoList(dbConfig,dbName,tableName);
        return fieldInfoList;
    }

    public List<FieldInfo> getFieldInfoList(DbConfig dbConfig,String dbName, String tableName) throws SQLException, ClassNotFoundException {
        dbName = Optional.ofNullable(dbName).orElse("");
        if(dbName.length() == 0) throw new BusinessException("没有指定数据库名称，无法获取表信息！");
        tableName = Optional.ofNullable(tableName).orElse("");
        if(tableName.length() == 0) throw new BusinessException("没有指定表名，无法获取字段信息！");

        List<FieldInfo> fieldInfoList = MySQLUtils.getFieldInfoList(dbConfig,dbName,tableName);
        return fieldInfoList;
    }

    public List<TableInfo> getTableInfoListNoFields(DbConfig dbConfig,String dbName) throws SQLException, ClassNotFoundException {
        dbName = Optional.ofNullable(dbName).orElse("");
        if(dbName.length() == 0) throw new BusinessException("没有指定数据库名称，无法获取表信息！");

        List<TableInfo> tableInfoList = MySQLUtils.getTableInfoListNoFiels(dbConfig,dbName);
        return tableInfoList;
    }
}
